package com.yong.android.framework.view;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.yong.android.framework.R;

import org.jetbrains.annotations.Nullable;


public class BaseToolbar extends LinearLayout {
    private static final String TAG = "FreeToolbarBar";
    private ImageView ivBack;
    private TextView tvTitle;
    private TextView actionText;


    public BaseToolbar(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BaseToolBar);
        int resId = a.getResourceId(R.styleable.BaseToolBar_layoutId, R.layout.toolbar_common);
        inflate(context, resId, this);
        ivBack = (ImageView) findViewById(R.id.toolbar_back);
        tvTitle = (TextView) findViewById(R.id.toolbar_title);
        actionText = (TextView) findViewById(R.id.toolbar_action_text);
        setOnBackClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity = getActivity();
                if (activity == null) {
                } else {
                    activity.finish();
                }
            }
        });
        String title = a.getString(R.styleable.BaseToolBar_freeTitle);
        if (!TextUtils.isEmpty(title)){
            setTitle(title);
        }
        setBackIconVisible(a.getBoolean(R.styleable.BaseToolBar_isShowBack, true));
        a.recycle();

    }

    /**
     * 设置返回键监听事件
     *
     * @param onClickListener
     */
    public void setOnBackClickListener(OnClickListener onClickListener) {
        if (ivBack != null) {
            ivBack.setOnClickListener(onClickListener);
        }

    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }





    /**
     * 设置返回键可见性
     *
     * @param visible
     */
    public void setBackIconVisible(boolean visible) {
        if (ivBack != null) {
            ivBack.setVisibility(visible ? VISIBLE : GONE);
        }
    }

    /**
     * 设置标题
     *
     * @param resId 文本资源文件
     */
    public void setTitle(@StringRes int resId) {
        if (tvTitle != null) {
            tvTitle.setText(resId);
        }
    }

    /**
     * 设置默认的动作文本可见性
     *
     * @param visible
     */
    public void setActionTextVisible(boolean visible) {
        if (actionText != null) {
            actionText.setVisibility(visible ? VISIBLE : GONE);
        }
    }

    /**
     * 设置默认的动作文本
     *
     * @param resId
     */
    public void setActionText(@StringRes int resId) {
        if (actionText != null) {
            actionText.setText(resId);
        }
    }

    /**
     * 设置默认动作文本点击事件
     *
     * @param onClickListener
     */
    public void setActionTextClickListener(OnClickListener onClickListener) {
        if (actionText != null) {
            actionText.setOnClickListener(onClickListener);
        }
    }

    private Activity getActivity() {
        Context context = getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return null;
    }
}
